﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IntroduceCompany.Util;
using IntroduceCompany.Singleton;

namespace IntroduceCompany.Controllers
{
    [HandleError]
    [IntroduceCompany.Models.CheckModule(ConstantClass.Backend_Language_MenuID)]
    public class LanguageController : BaseController
    {
        //
        // GET: /Language/

        /// <summary>
        /// Change Language for AdminPage
        /// </summary>
        /// <param name="languageID"></param>
        /// <param name="currentUrl"></param>
        /// <returns></returns>
        public ActionResult ChangeLanguage(String languageID, String currentUrl)
        {
            if (Request.Cookies[ConstantClass.cooLanguage] == null)
            {
                HttpCookie cookie = new HttpCookie(ConstantClass.cooLanguage, languageID);
                Response.Cookies.Add(cookie);
                cookie.Expires = DateTime.Now.AddDays(ConstantClass.cooLanguageTimeLife);
            }
            else
            {
                Response.Cookies[ConstantClass.cooLanguage].Value = languageID;
            }

            return Redirect(currentUrl);
            //return RedirectToAction("ViewAllMenuList", "MenuList");
        }


        //public void ChangeLanguage2(String languageID)
        //{
        //    if (Request.Cookies[ConstantClass.cooLanguage] == null)
        //    {
        //        HttpCookie cookie = new HttpCookie(ConstantClass.cooLanguage, languageID);
        //        Response.Cookies.Add(cookie);
        //        cookie.Expires = DateTime.Now.AddDays(1);
        //    }
        //    else
        //    {
        //        Response.Cookies[ConstantClass.cooLanguage].Value = languageID;
        //    }
        //}


        [Authorize(Roles = ConstantClass.autLanguage)]
        public ActionResult LanguageList()
        {

            return View("/Views/Backend/Language/LanguageList.aspx", DataSingleton.GetLanguageModels().GetLanguageList_Backend());
        }


        /// <summary>
        /// Change Product state that is visible or invisible
        /// </summary>
        /// <param name="menuListID"></para
        /// <param name="menuVisible"></param>
        /// <returns></returns>
        [Authorize(Roles = ConstantClass.autLanguage)]
        public ActionResult ChangeLanguageVisible(String languageID, bool languageVisible)
        {
            DataSingleton.GetLanguageModels().ChangeLanguageVisible(languageID, languageVisible);
            return RedirectToAction("LanguageList", "Language");
        }


        [Authorize(Roles = ConstantClass.autLanguage)]
        public ActionResult LanguageCreate()
        {

            return View("/Views/Backend/Language/LanguageCreate.aspx");
        }


        [Authorize(Roles = ConstantClass.autLanguage)]
        [HttpPost, ValidateInput(false), ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult LanguageCreate(Language language, HttpPostedFileBase uploadFile)
        {

            string newImageMenu = "";
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                //Upload Image
                newImageMenu = DataSingleton.GetGeneralModels().LanguageUploadImage(uploadFile, Request.PhysicalApplicationPath.ToString(), "");
            }


            if (ModelState.IsValid && TryUpdateModel(language))
            {

                if (DataSingleton.GetLanguageModels().CheckLanguageIDExisted(language.LanguageID))
                {
                    ModelState.AddModelError("NoticePage", "Mã ngôn ngữ đã được sử dụng.");
                }
                else
                {
                    if (DataSingleton.GetLanguageModels().CreateLanguage(language, newImageMenu))
                    {
                        ModelState.Clear();
                        ModelState.AddModelError("NoticePage", "Ngôn ngữ đã được thêm mới.");
                        return View("/Views/Backend/Language/LanguageCreate.aspx");
                    }
                    else
                    {
                        ModelState.AddModelError("NoticePage", "Có lỗi khi thêm ngôn ngữ. Có thể mã ngôn ngữ đã được sử dụng.");
                    }
                }
            }
            return View("/Views/Backend/Language/LanguageCreate.aspx", language);
        }


        [Authorize(Roles = ConstantClass.autLanguage)]
        public ActionResult LanguageEdit(String languageID)
        {
            return View("/Views/Backend/Language/LanguageEdit.aspx", DataSingleton.GetLanguageModels().GetLanguageDetails(languageID));
        }

        [Authorize(Roles = ConstantClass.autLanguage)]
        [HttpPost, ValidateInput(false),ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult LanguageEdit(Language language, HttpPostedFileBase uploadFile, String FlagImage)
        {
            string newImageMenu = "";
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                //Upload Image
                newImageMenu = DataSingleton.GetGeneralModels().LanguageUploadImage(uploadFile, Request.PhysicalApplicationPath.ToString(), FlagImage);
            }
            if (ModelState.IsValid && TryUpdateModel(language))
            {
                if (DataSingleton.GetLanguageModels().EditLanguage(language, newImageMenu))
                {
                    ModelState.AddModelError("NoticePage", "Ngôn ngữ đã được cập nhật.");
                }
                else
                {
                    ModelState.AddModelError("NoticePage", "Có lỗi khi cập nhật ngôn ngữ.");
                }
            }
            return View("/Views/Backend/Language/LanguageEdit.aspx", DataSingleton.GetLanguageModels().GetLanguageDetails(language.LanguageID));
        }

        [Authorize(Roles = ConstantClass.autLanguage)]
        public ActionResult LanguageDelete(String languageID)
        {
            DataSingleton.GetLanguageModels().DeleteLanguage(languageID, (Request.PhysicalApplicationPath.ToString() + "Image/Flag/"));
            return RedirectToAction("LanguageList", "Language");
        }

    }
}
